if __debug__: # invoke python with -O or -OO to unset
    from SimPy.SimulationTrace import *
else:
    from SimPy.Simulation import *

import Universe
import Person

class Model(Simulation):
    '''The simulation model'''

    def __init__(self, n, k, p, t, timezones):
        '''Initialize the model'''

        Simulation.__init__(self)
        self.verbose = True
        self.n = n;
        self.k = k;
        self.p = p;
        self.t = t;
        self.timezones = timezones;
        self.univ = Universe.Universe(self, self.n, self.k, self.p, self.timezones);
        # self.report("Connections:")
        # self.univ.connections_show()

    def report(self, text):
        '''Report with timestamp'''
        
        secs = self.now() % 60
        mins = (self.now() / 60) % 60
        hours = (self.now() / 60) / 60
        
        if self.verbose:
            print "%d\t%02d:%02d:%02d\t%s" % (self.now(), hours, mins, secs,
                                              text)

    def runModel(self):
        '''The starting point of the simulation'''
        
        self.initialize()

        for p in self.univ.persons:
            self.activate(p, p.run());
            # self.report("Person %s activated." % p.name);
        # self.report("Simulation initialized, will run for %d days." % self.t);
        self.simulate(until = 24 * 60 * 60 * self.t);

if __name__ == '__main__':
    import sys;

    if len(sys.argv) < 6:
        print '''Usage: %s n k p t zones
where the options are as follows:

    - n : number of users
    - k : size of your close network (must be less than n)
    - p : probability of you having a relation to a random person
          outside your close network.  Keep in mind that n*p controls
          the size of your users' network of strangers.
    - zones: which timezone configuration to run,
             0 = everyone in the same timezone
             1 = even distribution across continental US timezones
    - t : simulation run time in days
''' % sys.argv[0];
        sys.exit(1);
    
    try:
        n = int(sys.argv[1]);
        k = int(sys.argv[2]);
        p = float(sys.argv[3]);
        zones = int(sys.argv[4]);
        t = int(sys.argv[5]);
    except:
        print "Some of your arguments are wrong, please fix!";
        sys.exit(1);

    cm = Model(n, k, p, t, zones);
    cm.report("Model started.")
    cm.runModel()
    cm.report("Model stopped.")
